package com.hzhk.happyrent.network;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hzhk.happyrent.bean.rent.BaseBean;
import com.hzhk.happyrent.bean.rent.TypesModel;
import com.hzhk.happyrent.ui.dialog.LoadingDialog;
import com.tamic.novate.NovateResponse;
import com.tamic.novate.callback.ResponseCallback;
import com.tamic.novate.util.LogWraper;
import com.tamic.novate.util.NetworkUtil;
import com.tamic.novate.util.Utils;
import org.json.JSONObject;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.ResponseBody;

/**
 * RxGenericsCallback<T> 泛型回调
 * Created by Tamic on 2016/6/23.
 */
public abstract class MyResponseCallback<T> extends ResponseCallback<T, BaseBean> {

    protected T dataResponse = null;
    protected int code = -1;
    protected String msg = "";
    private Dialog dialog;
    private Activity context;

    public MyResponseCallback(Activity context) {
        this.context=context;
        dialog=new LoadingDialog(context,"");
    }

    @Override
    public void onStart(Object tag) {
        super.onStart(tag);
        if (!NetworkUtil.isNetworkAvailable(context)) {
            Toast.makeText( context, "网络异常",Toast.LENGTH_SHORT).show();
            onCompleted(tag);
            return;
        }
        if(dialog!=null){
            if(dialog.isShowing()){
                dialog.dismiss();
            }
            dialog.show();
        }
    }


    @Override
    public void onCompleted(Object tag) {
        super.onCompleted(tag);
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public T onHandleResponse(ResponseBody response) throws Exception {
        dataResponse= (T) response;
        return (T) response;
    }



    @Override
    public void onNext(final Object tag, Call call, T response) {

        if (Utils.checkMain()) {
            onNext(tag, code, msg, dataResponse);
        } else {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    onNext(tag, code, msg, dataResponse);
                }
            });
        }
    }

    public abstract void onNext(Object tag, int code, String message, T response);

}